package com.rlz.framemm.util;

import android.annotation.SuppressLint;
import android.content.Context;

import com.rlz.libs.log.P;
import com.rlz.base.app.Base;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by RLZ
 * on 4/19/21
 */
public class SoFile {

    /**
     * 需要拷贝的文件目录
     */
    public static final String UP_SO_ZIP_PATH =
            Base.obtain().
                    getExternalFilesDir(null)
                    + File.separator + "lib_so";

    /**
     * 加载 so 文件
     */
    public static void loadSoFile(Context context) {
        File dir = context.getDir("libs", Context.MODE_PRIVATE);
        if (!isLoadSoFile(dir)) {
            copy(dir.getAbsolutePath());
        } else {
            loadSo();
        }
    }

    /**
     * 判断 so 文件是否存在
     */
    public static boolean isLoadSoFile(File dir) {
        File[] currentFiles;
        currentFiles = dir.listFiles();
        boolean hasSoLib = false;
        if (currentFiles == null) {
            return false;
        }
        for (File currentFile : currentFiles) {
            if (currentFile.getName().contains("libijkplayer")) {
                hasSoLib = true;
            }
        }
        return hasSoLib;
    }

    /**
     * 拷贝文件
     */
    private static void copy(String toFile) {
        //要复制的文件目录
        File[] currentFiles;
        File root = new File(UP_SO_ZIP_PATH);
        if (!root.exists()) {
            return;
        }

        currentFiles = root.listFiles();

        File targetDir = new File(toFile);

        //创建目录
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        if (currentFiles == null) return;

        for (File currentFile : currentFiles) {
            if (currentFile.isDirectory()) {
                //如果当前项为子目录 进行递归
                copy(toFile + currentFile.getName() + "/");
            } else {
                //如果当前项为文件则进行文件拷贝
                if (currentFile.getName().contains(".so")) {
                    P.e("当前SO ->名字" + currentFile.getName());
                    copySdcardFile(currentFile.getPath(), toFile + File.separator + currentFile.getName());
                }
            }
        }

        if (currentFiles.length > 0) loadSo();

    }

    //文件拷贝
    private static void copySdcardFile(String fromFile, String toFile) {
        try {
            FileInputStream fileFrom = new FileInputStream(fromFile);
            FileOutputStream fileTo = new FileOutputStream(toFile);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fileFrom.read(buffer)) != -1)
                byteStream.write(buffer, 0, len);

            fileTo.write(byteStream.toByteArray());

            byteStream.close();
            fileTo.close();
            fileFrom.close();
        } catch (Exception ex) {
            P.e("file copy error   " + ex.getMessage());
        }
    }


    @SuppressLint("UnsafeDynamicallyLoadedCode")
    private static void loadSo() {
        File dir = Base.obtain().getDir("libs", Context.MODE_PRIVATE);
        File[] currentFiles = dir.listFiles();
        if (currentFiles != null && currentFiles.length > 0) {
            for (File currentFile : currentFiles) {
                P.d("SO 文件名 ->" + currentFile.getName() + "--->路劲:--->" + currentFile.getAbsolutePath());

                System.load(currentFile.getAbsolutePath());
            }

            P.d("加载的SO 个数 ->" + currentFiles.length);
        }
    }

}

